using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MicroMsg.Manager
{
	public class SnsPageSource
	{
		private const string TAG = "SnsPageSource";
		private const int pageSize = 40;
		public string strUserName;
		private SnsPage curPage;
		private List<ulong> page_ids;
		private List<ulong> page_ids_fresh = new List<ulong>();
		private uint firstPageRequestTime;
		private bool bRefreshAllpage;
		private int requestRefreshItem;
		private SnsPageScene scene;
		private List<ulong> sending_ids;
		private List<ulong> sent_ids;
		private List<ulong> show_ids = new List<ulong>();
		private List<int> allowStyleList;
		private List<ulong> ignore_ids = new List<ulong>();
		private List<ulong> delete_ids = new List<ulong>();
		private SnsUserCatalog eUserType;
		private bool bCheckUserType;
		public bool bCacheMode;
		private List<SnsObject> cacheList;
		private Action<SnsPageSource> rspCallback;
		private static SnsPageSource snsPageSource_0;
		public SnsPageSource(string name)
		{
			if (string.IsNullOrEmpty(name))
			{
				DebugEx.debugBreak();
			}
			this.strUserName = name;
			this.curPage = StorageMgr.snsPage.method_1(name);
			if (this.curPage == null)
			{
				this.curPage = new SnsPage
				{
					strUserName = name
				};
			}
			this.page_ids = this.curPage.regionInfo.getList();
			this.delete_ids = SnsAsyncMgr.getDeleteIDS();
			this.scene = new SnsPageScene(name, new SnsPageSceneCallBack(this.onSnsPageSceneResponse));
			if (SnsPageMgr.needShowSendingObj(this.strUserName))
			{
				this.sending_ids = SnsInfoMgr.getSendingObjectID();
			}
			if (SnsPageMgr.needShowSentObj(this.strUserName))
			{
				this.sent_ids = SnsInfoMgr.getSentObjectID();
			}
			this.verifyIDS();
			this.makeShowIDS();
		}
		public void checkUserType(bool checkUser)
		{
			this.bCheckUserType = checkUser;
		}
		public void setAllowStyle(List<TimeLineObjStyle> allow_styles)
		{
			this.initIgnoreIDS(allow_styles);
			this.makeShowIDS();
		}
		public void setResponeCallback(Action<SnsPageSource> action_0)
		{
			this.rspCallback = action_0;
		}
		public void onSendingObjChanged()
		{
			if (SnsPageMgr.needShowSendingObj(this.strUserName))
			{
				this.sending_ids = SnsInfoMgr.getSendingObjectID();
			}
			if (SnsPageMgr.needShowSentObj(this.strUserName))
			{
				this.sent_ids = SnsInfoMgr.getSentObjectID();
			}
			this.verifySentIDS();
			this.makeShowIDS();
		}
		public void onSnsRecyced()
		{
			this.verifyIDS();
			this.makeShowIDS();
		}
		public void onSnsDelete()
		{
			this.delete_ids = SnsAsyncMgr.getDeleteIDS();
			this.makeShowIDS();
		}
		private void addToCache(IList<SnsObject> objList)
		{
			if (objList == null || objList.get_Count() <= 0)
			{
				return;
			}
			if (this.cacheList == null)
			{
				this.cacheList = Enumerable.ToList<SnsObject>(Enumerable.Where<SnsObject>(objList, (SnsObject snsObject_0) => snsObject_0.NoChange == 0u));
				return;
			}
			objList = Enumerable.ToList<SnsObject>(Enumerable.Where<SnsObject>(objList, (SnsObject snsObject_0) => snsObject_0.NoChange == 0u));
			using (IEnumerator<SnsObject> enumerator = objList.GetEnumerator())
			{
				SnsObject snsObject_0;
				while (enumerator.MoveNext())
				{
					snsObject_0 = enumerator.get_Current();
					SnsObject snsObject = Enumerable.FirstOrDefault<SnsObject>(Enumerable.Where<SnsObject>(this.cacheList, (SnsObject snsObject_1) => snsObject_1.Id == snsObject_0.Id));
					if (snsObject != null)
					{
						int num = this.cacheList.IndexOf(snsObject);
						this.cacheList.set_Item(num, snsObject_0);
					}
					else
					{
						this.cacheList.Add(snsObject_0);
					}
				}
			}
			this.cacheList = Enumerable.ToList<SnsObject>(Enumerable.OrderByDescending<SnsObject, ulong>(this.cacheList, (SnsObject snsObject_0) => snsObject_0.Id));
		}
		public List<SnsObject> getCacheList()
		{
			return this.cacheList;
		}
		private void initIgnoreIDS(List<TimeLineObjStyle> allow_styles)
		{
			if (allow_styles == null || allow_styles.get_Count() <= 0)
			{
				return;
			}
			this.allowStyleList = Enumerable.ToList<int>(Enumerable.Select<TimeLineObjStyle, int>(allow_styles, (TimeLineObjStyle timeLineObjStyle_0) => (int)timeLineObjStyle_0));
			if (this.allowStyleList == null || this.page_ids.get_Count() <= 0)
			{
				return;
			}
			this.ignore_ids.Clear();
			DebugEx.getTimeSpan();
			Dictionary<ulong, int> objectStyle = StorageMgr.snsInfo.getObjectStyle(this.page_ids);
			List<ulong> list = Enumerable.ToList<ulong>(Enumerable.Select<KeyValuePair<ulong, int>, ulong>(Enumerable.Where<KeyValuePair<ulong, int>>(objectStyle, (KeyValuePair<ulong, int> keyValuePair_0) => !this.allowStyleList.Contains(keyValuePair_0.get_Value())), (KeyValuePair<ulong, int> keyValuePair_0) => keyValuePair_0.get_Key()));
			if (list.get_Count() <= 0)
			{
				return;
			}
			this.ignore_ids.AddRange(list);
		}
		private void addToIgnoreIDS(List<SnsInfo> snsList)
		{
			if (this.allowStyleList == null || snsList == null || snsList.get_Count() <= 0)
			{
				return;
			}
			List<ulong> list = Enumerable.ToList<ulong>(Enumerable.Select<SnsInfo, ulong>(Enumerable.Where<SnsInfo>(snsList, (SnsInfo snsInfo_0) => !this.allowStyleList.Contains(snsInfo_0.nObjectStyle)), (SnsInfo snsInfo_0) => snsInfo_0.nObjectID));
			if (list.get_Count() <= 0)
			{
				return;
			}
			this.ignore_ids.AddRange(list);
		}
		private void verifyIDS()
		{
			List<ulong> list = null;
			if (this.page_ids.get_Count() <= 0)
			{
				this.curPage.strFirstPageMd5 = null;
			}
			else
			{
				DebugEx.getTimeSpan();
				list = StorageMgr.snsInfo.getObjectID();
				List<ulong> list2 = Enumerable.ToList<ulong>(Enumerable.Except<ulong>(Enumerable.Except<ulong>(this.page_ids, this.delete_ids), list));
				if (list2.get_Count() > 0)
				{
					this.curPage.regionInfo.breakTailAt(Enumerable.Max<ulong>(list2));
					this.page_ids = this.curPage.regionInfo.getList();
					this.curPage.strFirstPageMd5 = null;
				}
			}
			if (this.sending_ids != null && this.sending_ids.get_Count() > 0)
			{
				if (list == null)
				{
					list = StorageMgr.snsInfo.getObjectID();
				}
				List<ulong> list2 = Enumerable.ToList<ulong>(Enumerable.Except<ulong>(this.sending_ids, list));
				if (list2.get_Count() > 0)
				{
					SnsInfoMgr.delSendingObjectID(list2);
					this.sending_ids = SnsInfoMgr.getSendingObjectID();
				}
			}
			this.verifySentIDS();
		}
		private void verifySentIDS()
		{
			if (this.sent_ids != null && this.sent_ids.get_Count() > 0 && this.page_ids.get_Count() > 0)
			{
				this.sent_ids = Enumerable.ToList<ulong>(Enumerable.Where<ulong>(this.sent_ids, (ulong ulong_0) => ulong_0 > this.page_ids.get_Item(0)));
			}
		}
		private void makeShowIDS()
		{
			this.show_ids.Clear();
			if (this.sending_ids != null && this.sending_ids.get_Count() > 0)
			{
				this.show_ids.AddRange(this.sending_ids);
			}
			if (this.sent_ids != null && this.sent_ids.get_Count() > 0)
			{
				this.show_ids.AddRange(this.sent_ids);
			}
			this.show_ids.AddRange(this.page_ids);
			if (this.ignore_ids.get_Count() > 0)
			{
				this.show_ids = Enumerable.ToList<ulong>(Enumerable.Except<ulong>(this.show_ids, this.ignore_ids));
			}
			if (this.delete_ids.get_Count() > 0)
			{
				this.show_ids = Enumerable.ToList<ulong>(Enumerable.Except<ulong>(this.show_ids, this.delete_ids));
			}
		}
		public int count()
		{
			return this.show_ids.get_Count();
		}
		public List<SnsInfo> getListNearbyObjectID(ulong maxObjectID, int int_0)
		{
			int num;
			if (maxObjectID == 0uL)
			{
				num = 0;
			}
			else
			{
				num = this.show_ids.IndexOf(maxObjectID);
				if (num < 0)
				{
					DebugEx.debugBreak();
					return null;
				}
				num++;
			}
			List<ulong> list = Enumerable.ToList<ulong>(Enumerable.Take<ulong>(Enumerable.Skip<ulong>(this.show_ids, num), int_0));
			if (list != null && list.get_Count() > 0)
			{
				List<SnsInfo> list2 = SnsInfoMgr.getList(list);
				if (list2.get_Count() != int_0)
				{
					List<ulong> list3 = Enumerable.ToList<ulong>(Enumerable.Select<SnsInfo, ulong>(list2, (SnsInfo snsInfo_0) => snsInfo_0.nObjectID));
					List<ulong> list4 = Enumerable.ToList<ulong>(Enumerable.Except<ulong>(list, list3));
					if (list4.get_Count() > 0)
					{
						this.dump_ids(list4);
						DebugEx.debugBreak();
					}
				}
				return list2;
			}
			return null;
		}
		public List<SnsInfo> getList(int start, int int_0)
		{
			List<ulong> list = Enumerable.ToList<ulong>(Enumerable.Take<ulong>(Enumerable.Skip<ulong>(this.show_ids, start), int_0));
			if (list.get_Count() <= 0)
			{
				return null;
			}
			List<SnsInfo> list2 = SnsInfoMgr.getList(list);
			if (list2 == null)
			{
				return null;
			}
			if (list2.get_Count() != list.get_Count())
			{
				List<ulong> list3 = Enumerable.ToList<ulong>(Enumerable.Select<SnsInfo, ulong>(list2, (SnsInfo snsInfo_0) => snsInfo_0.nObjectID));
				List<ulong> list4 = Enumerable.ToList<ulong>(Enumerable.Except<ulong>(list, list3));
				if (list4.get_Count() > 0)
				{
					this.dump_ids(list4);
				}
				DebugEx.debugBreak();
			}
			return list2;
		}
		public List<SnsInfo> getList()
		{
			if (this.show_ids.get_Count() <= 0)
			{
				return null;
			}
			return SnsInfoMgr.getList(this.show_ids);
		}
		public List<SnsInfo> syncList(List<ulong> list_0)
		{
			if (list_0 == null || list_0.get_Count() <= 0)
			{
				return null;
			}
			ulong num = Enumerable.Last<ulong>(list_0);
			int num2 = this.show_ids.IndexOf(num);
			if (num2 < 0)
			{
				return null;
			}
			List<ulong> list = Enumerable.ToList<ulong>(Enumerable.Take<ulong>(this.show_ids, num2));
			List<ulong> list2 = Enumerable.ToList<ulong>(Enumerable.Except<ulong>(list, list_0));
			if (list2.get_Count() <= 0)
			{
				return null;
			}
			return SnsInfoMgr.getList(list2);
		}
		private void getFirstPage()
		{
			if (this.scene.isPageLoading())
			{
				return;
			}
			this.page_ids_fresh.Clear();
			this.firstPageRequestTime = 0u;
			this.bRefreshAllpage = false;
			this.scene.getFirstPage(this.curPage.strFirstPageMd5);
		}
		private void getNextPage()
		{
			if (this.scene.isPageLoading())
			{
				return;
			}
			if (this.bRefreshAllpage)
			{
				return;
			}
			if (this.page_ids_fresh.get_Count() <= 0)
			{
				return;
			}
			SnsRegion nextRegion = this.curPage.regionInfo.getNextRegion(Enumerable.Last<ulong>(this.page_ids_fresh), 40);
			if (nextRegion == null)
			{
				this.scene.getNextPage(Enumerable.Last<ulong>(this.page_ids_fresh), 0uL, 0u);
				return;
			}
			this.scene.getNextPage(Enumerable.Last<ulong>(this.page_ids_fresh), Enumerable.Last<ulong>(nextRegion.list_0), nextRegion.getTimeMin(0, 40));
		}
		public int getRefreshCount()
		{
			return this.page_ids_fresh.get_Count();
		}
		public void refreshFirsPage()
		{
			if (this.scene.isFirstPageLoading())
			{
				return;
			}
			if (this.scene.isPageLoading())
			{
				this.requestRefreshItem = 2147483647;
				return;
			}
			this.requestRefreshItem = 0;
			this.getFirstPage();
		}
		public void refreshItem(int itemIndex)
		{
			if (itemIndex < 0)
			{
				return;
			}
			if (this.bRefreshAllpage)
			{
				return;
			}
			if (this.requestRefreshItem == 2147483647)
			{
				return;
			}
			if (itemIndex >= 0 && itemIndex < this.show_ids.get_Count())
			{
				ulong num = this.show_ids.get_Item(itemIndex);
				if (SnsInfoMgr.isSendingObjectID(num))
				{
					return;
				}
				if (this.page_ids_fresh.Contains(num))
				{
					return;
				}
			}
			if (this.scene.isPageLoading())
			{
				this.requestRefreshItem = Math.Max(this.requestRefreshItem, itemIndex);
				return;
			}
			this.getNextPage();
		}
		public void refreshNextPage()
		{
			this.refreshItem(this.show_ids.get_Count() + 1);
		}
		public bool isPageRefreshing()
		{
			return this.scene.isPageLoading();
		}
		public void cancelRefresh()
		{
			this.requestRefreshItem = 0;
		}
		public bool isRefreshAllPageDone()
		{
			return this.bRefreshAllpage;
		}
		public bool contains(SnsInfo snsInfo_0)
		{
			return snsInfo_0 != null && this.show_ids.Contains(snsInfo_0.nObjectID);
		}
		public SnsUserCatalog getUserType()
		{
			return this.eUserType;
		}
		public void dump_ids(List<ulong> list_0)
		{
			string text = "ids=";
			using (List<ulong>.Enumerator enumerator = list_0.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ulong current = enumerator.get_Current();
					text = text + current + ",";
				}
			}
		}
		private List<ulong> getDeleteItem(List<ulong> ids_new, List<ulong> ids_old)
		{
			if (ids_old == null || ids_old.get_Count() <= 0)
			{
				return null;
			}
			if (ids_new != null && ids_new.get_Count() != 0)
			{
				return Enumerable.ToList<ulong>(Enumerable.Except<ulong>(ids_old, ids_new));
			}
			return ids_old;
		}
		private List<ulong> getDetachBySetPrivate(List<ulong> list_0)
		{
			if (list_0 != null && list_0.get_Count() > 0)
			{
				List<ulong> list = new List<ulong>();
				List<SnsInfo> list2 = SnsInfoMgr.getList(list_0);
				using (List<SnsInfo>.Enumerator enumerator = list2.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SnsInfo current = enumerator.get_Current();
						TimeLineObject timeLine = SnsInfoMgr.getTimeLine(current);
						if (timeLine != null && timeLine.nPrivate == 1)
						{
							list_0.Remove(current.nObjectID);
							list.Add(current.nObjectID);
						}
					}
				}
				return list;
			}
			return null;
		}
		private void pageDeleteItem(List<ulong> ids_new, List<ulong> ids_old)
		{
			List<ulong> deleteItem = this.getDeleteItem(ids_new, ids_old);
			if (deleteItem != null && deleteItem.get_Count() > 0)
			{
				if (this.strUserName == SnsPageMgr.TimeLineUserName)
				{
					this.getDetachBySetPrivate(deleteItem);
					if (deleteItem == null || deleteItem.get_Count() <= 0)
					{
						return;
					}
				}
				SnsInfoMgr.delList(deleteItem);
				return;
			}
		}
		private List<SnsInfo> do_notify(List<ulong> ids_notify, EventConst eventConst_0 = EventConst.ON_MGR_SNSPAGE_PAGE_ATTACH_ITEM)
		{
			List<SnsInfo> list = SnsInfoMgr.getList(ids_notify);
			if (list == null)
			{
				return null;
			}
			using (List<SnsInfo>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsInfo current = enumerator.get_Current();
					EventCenter.postCombineEvent(eventConst_0, current);
				}
			}
			return list;
		}
		private List<SnsInfo> pageAttachItemNotify(List<ulong> ids_new, List<ulong> ids_old)
		{
			if (ids_new == null || ids_new.get_Count() <= 0)
			{
				return null;
			}
			List<ulong> list;
			if (ids_old != null && ids_old.get_Count() > 0)
			{
				list = Enumerable.ToList<ulong>(Enumerable.Except<ulong>(ids_new, ids_old));
			}
			else
			{
				list = ids_new;
			}
			if (list != null && list.get_Count() > 0)
			{
				return this.do_notify(list, EventConst.ON_MGR_SNSPAGE_PAGE_ATTACH_ITEM);
			}
			return null;
		}
		private void tryGetPage()
		{
			if (this.requestRefreshItem == 0)
			{
				return;
			}
			if (this.requestRefreshItem == 2147483647)
			{
				this.requestRefreshItem = 0;
				this.getFirstPage();
				return;
			}
			if ((this.page_ids_fresh != null && this.requestRefreshItem < this.page_ids_fresh.get_Count()) || this.bRefreshAllpage)
			{
				this.requestRefreshItem = 0;
				return;
			}
			this.getNextPage();
		}
		private void tryGetUserType()
		{
			if (this.bCheckUserType && this.bRefreshAllpage && this.page_ids_fresh.get_Count() > 0 && this.eUserType == SnsUserCatalog.SnsUserPending)
			{
				this.scene.getNextPage(Enumerable.Last<ulong>(this.page_ids_fresh), 0uL, 0u);
				return;
			}
		}
		private void onSnsPageSceneResponse(SnsPageResponse snsPageResponse_0)
		{
			bool flag = false;
			if (snsPageResponse_0 == null)
			{
				return;
			}
			SnsPageMgr.setTimeStamp("get user page success");
			switch (snsPageResponse_0.type)
			{
			case PageRspType.firstPage:
			case PageRspType.firstPageVerify:
				if (snsPageResponse_0.nRetCode == 207)
				{
					this.bRefreshAllpage = true;
					this.firstPageRequestTime = snsPageResponse_0.pageRequestTime;
					this.curPage.nObjectTotal = snsPageResponse_0.total;
					this.curPage.strFirstPageMd5 = snsPageResponse_0.strFirstPageMd5;
				}
				else
				{
					this.bRefreshAllpage = false;
					this.firstPageRequestTime = snsPageResponse_0.pageRequestTime;
					this.curPage.nObjectTotal = snsPageResponse_0.total;
					this.curPage.strFirstPageMd5 = snsPageResponse_0.strFirstPageMd5;
					if (snsPageResponse_0.type == PageRspType.firstPageVerify && snsPageResponse_0.objList.get_Count() <= 0)
					{
						this.page_ids_fresh = Enumerable.ToList<ulong>(Enumerable.Take<ulong>(this.page_ids, 40));
						if (this.page_ids_fresh.get_Count() < 40)
						{
							this.bRefreshAllpage = true;
							goto IL_273;
						}
						goto IL_273;
					}
					else
					{
						this.page_ids_fresh.Clear();
						flag = true;
					}
				}
				break;
			case PageRspType.nextPage:
			case PageRspType.nextPagVerify:
				this.eUserType = ((snsPageResponse_0.nRetCode == 203) ? SnsUserCatalog.SnsUserStranger : SnsUserCatalog.SnsUserFriend);
				break;
			}
			if (snsPageResponse_0.objList.get_Count() <= 0)
			{
				this.bRefreshAllpage = true;
			}
			else
			{
				List<ulong> list = Enumerable.ToList<ulong>(Enumerable.OrderByDescending<ulong, ulong>(Enumerable.Select<SnsObject, ulong>(snsPageResponse_0.objList, (SnsObject snsObject_0) => snsObject_0.Id), (ulong ulong_0) => ulong_0));
				this.dump_ids(list);
				this.page_ids_fresh.AddRange(list);
			}
			if (this.bRefreshAllpage)
			{
				this.curPage.regionInfo.reset(this.page_ids_fresh, this.firstPageRequestTime);
			}
			else
			{
				this.curPage.regionInfo.replaceFirstRegion(this.page_ids_fresh, this.firstPageRequestTime);
			}
			this.curPage.regionsInfoFlush();
			if (this.bCacheMode)
			{
				this.addToCache(snsPageResponse_0.objList);
				if (this.rspCallback != null)
				{
					this.rspCallback.Invoke(this);
				}
				return;
			}
			StorageMgr.snsPage.update(this.curPage);
			SnsInfoMgr.updateList(snsPageResponse_0.objList);
			List<ulong> list2 = this.curPage.regionInfo.getList();
			List<SnsInfo> snsList = this.pageAttachItemNotify(list2, this.page_ids);
			this.addToIgnoreIDS(snsList);
			this.pageDeleteItem(list2, this.page_ids);
			this.page_ids = list2;
			if (flag)
			{
				this.verifySentIDS();
			}
			IL_273:
			this.makeShowIDS();
			this.tryGetPage();
			this.tryGetUserType();
			if (this.rspCallback != null)
			{
				this.rspCallback.Invoke(this);
			}
		}
		public static void test()
		{
			if (SnsPageSource.snsPageSource_0 == null)
			{
				SnsPageSource.snsPageSource_0 = new SnsPageSource(SnsPageMgr.TimeLineUserName);
			}
			SnsPageSource.snsPageSource_0.getFirstPage();
		}
		public static void test1()
		{
			List<ulong> list = new List<ulong>();
			list.Add(1uL);
			list.Add(2uL);
			list.Add(3uL);
			list.Add(4uL);
			List<ulong> ids_new = list;
			List<ulong> list2 = new List<ulong>();
			list2.Add(4uL);
			list2.Add(5uL);
			list2.Add(6uL);
			List<ulong> ids_old = list2;
			List<ulong> list3 = new List<ulong>();
			list3.Add(1uL);
			SnsPageSource snsPageSource = new SnsPageSource(SnsPageMgr.TimeLineUserName);
			snsPageSource.pageAttachItemNotify(ids_new, ids_old);
		}
	}
}
